package com.common.entity;

import com.beans.ErrorStatusEnum;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author 平台组提供
 * @date 2019年10月27日 下午9:59:27
 */
public class R extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String CODE = "code";
    public static final String MESSAGE = "message";
    public static final String DATA = "data";

    public R() {
        put(CODE, 200);
        put(MESSAGE, ErrorStatusEnum.getMsgByCode(200));
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String msg) {
        R r = new R();
        r.put(MESSAGE, msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok(Object data) {
        R r = new R();
        r.put(DATA, data);
        return r;
    }

    public static R error(int code) {
        R r = new R();
        r.put(CODE, code);
        r.put(MESSAGE, ErrorStatusEnum.getMsgByCode(code));
        return r;
    }

    public static R error(String msg) {
        R r = new R();
        r.put(CODE, msg);
        r.put(MESSAGE, msg);
        return r;
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put(CODE, code);
        r.put(MESSAGE, msg);
        return r;
    }

    public static R error(int code, String msg, Object data) {
        R r = new R();
        r.put(CODE, code);
        r.put(MESSAGE, msg);
        r.put(DATA, data);
        return r;
    }


    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
